package com.jsyh.maidu.activity.me;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jsyh.maidu.R;
import com.jsyh.maidu.adapter.home.PayTypeAdapter;
import com.jsyh.maidu.model.OrderSubmitMsg;
import com.jsyh.maidu.model.PaymentType;
import com.jsyh.maidu.model.SqOrderMsg;
import com.jsyh.maidu.presenter.OrderSubmitPresenter;
import com.jsyh.maidu.presenter.SqPayMessagePresenter;
import com.jsyh.maidu.views.OrderSubmitView;
import com.jsyh.maidu.views.SqPayMessageView;
import com.jsyh.shopping.uilibrary.views.MyListView;

import java.util.List;

public class SQPaySecondActivity extends Activity implements SqPayMessageView,AdapterView.OnItemClickListener,View.OnClickListener,OrderSubmitView{

    TextView tv_payprice;
    TextView tv_consumeprice;
    TextView tv_relprice,tv_balanca;
    TextView tv_title;
    Button btn_submit;
    ImageView iv_back;
    MyListView lv_paytype;
    RelativeLayout rl_payway;
    String online;

   /* ImageView iv_select;*/

    SqPayMessagePresenter sqpaymsgpresenter;
    OrderSubmitPresenter ordersubmitpresenter;
    PayTypeAdapter paytypeadapter;
   List<PaymentType> paytypelist;

    String sid,price;
    int paytypenum=-1;
    int paytypeid=-1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqpay_second);

        sid=getIntent().getStringExtra("suppid");
        price=getIntent().getStringExtra("price");



        intenView();
        sqpaymsgpresenter=new SqPayMessagePresenter(this);
        sqpaymsgpresenter.getSqOrderMsg(this,sid,price);




    }

    public void intenView()
    {
        tv_consumeprice= (TextView) findViewById(R.id.tv_SQS_consumeprice);
        tv_payprice= (TextView) findViewById(R.id.tv_SQS_payprice);
        tv_relprice= (TextView) findViewById(R.id.tv_SQS_relprice);
        lv_paytype= (MyListView) findViewById(R.id.lv_SQS_paytype);
        tv_title= (TextView) findViewById(R.id.title);
        iv_back= (ImageView) findViewById(R.id.back);
        btn_submit= (Button) findViewById(R.id.btn_SQS_submit);
        tv_balanca= (TextView) findViewById(R.id.tv_SQS_balance);
        rl_payway= (RelativeLayout) findViewById(R.id.rl_sqs_payway);

        tv_title.setText(getIntent().getStringExtra("title"));
        paytypeadapter=new PayTypeAdapter(this,R.layout.paytype_item);

        iv_back.setOnClickListener(this);
        btn_submit.setOnClickListener(this);
      /*  ll_paytype= (LinearLayout) findViewById(R.id.ll_SQS_payitem);*/


    }

    @Override
    public void getPayMessage(SqOrderMsg response) {
        tv_payprice.setText("此单需支付总金额:"+response.getData().getPay_moneyALL());
        tv_relprice.setText("此单您还需支付金额:"+response.getData().getPay_money());
        tv_consumeprice.setText("此单可用消费券:"+response.getData().getVoued_money());
        tv_balanca.setText("账户余额消费金额："+response.getData().getYck_money());
        if (response.getData().getOnline_zf().equals("0"))
        {
            online="0";
            lv_paytype.setVisibility(View.GONE);
            rl_payway.setVisibility(View.GONE);
        }else {
            online="1";
            rl_payway.setVisibility(View.VISIBLE);
            lv_paytype.setVisibility(View.VISIBLE);
            lv_paytype.setAdapter(paytypeadapter);
            lv_paytype.setOnItemClickListener(this);
            paytypelist= response.getData().getPayment_list();
            paytypeadapter.addAll(paytypelist);
        }




    }




    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {


            if (paytypenum<0)
            {
                paytypeadapter.clear();
                paytypenum=position;
                paytypelist.get(position).setChecked(true);
                paytypeid=paytypelist.get(position).getPay_id();
                paytypeadapter.addAll(paytypelist);

            }else
            {
                if (!paytypelist.get(position).isChecked())
                {
                    paytypeadapter.clear();
                    paytypelist.get(paytypenum).setChecked(false);
                    paytypenum=position;
                    paytypelist.get(position).setChecked(true);
                    paytypeid=paytypelist.get(position).getPay_id();

                    paytypeadapter.addAll(paytypelist);
                }

            }
        }

    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.back:
                finish();
                break;
            case R.id.btn_SQS_submit:
                ordersubmitpresenter=new OrderSubmitPresenter(this);

                ordersubmitpresenter.getOSubmitMsg(this,price,sid,online,paytypeid+"");
                break;
        }
    }

    @Override
    public void getOrserSubmitMsg(OrderSubmitMsg response) {
        Intent IntentOS=new Intent(SQPaySecondActivity.this,SqPayThirdActivity.class);
        Bundle bundle=new Bundle();
        bundle.putSerializable("orderMsg",response);
        IntentOS.putExtra("sid",sid);
        if (response.getData().getOnline_zf()==0)
        {
            IntentOS.putExtra("payor","0");

        }else {
            IntentOS.putExtra("payor","1");
        }
        IntentOS.putExtras(bundle);
        startActivity(IntentOS);
    }
}
